package com.hm.system.web;

import java.io.IOException;
import java.io.PrintWriter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.hm.common.annotation.AuthenticationAnnotation;
import com.hm.common.annotation.RequestPlanAnnotation;
import com.hm.common.common.CommonUtil;
import com.hm.common.model.ControllerResult;
import com.hm.common.model.PageInfo;
import com.hm.system.mongo.domain.def.SysMenuDef;
import com.hm.system.mongo.vo.system.SysMenuInfoVo;
import com.hm.system.share.service.mongo.system.SysMenuInfoService;
import com.hm.system.util.SystemConfigDef.Api.Rest;
import com.hm.system.web.su.BaseController;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author shishun.wang
 * @date 下午4:37:06 2016年6月1日
 * @version 1.0
 * @describe 系统菜单
 */
@ResponseBody
@RestController
@RequestMapping(value = Rest.Sys.REST_SYS_MENU_INFO)
// @CrossOrigin(methods = { RequestMethod.GET, RequestMethod.OPTIONS,
// RequestMethod.POST, RequestMethod.PUT })
public class SysMenuInfoController extends BaseController {

	@Autowired
	private SysMenuInfoService sysMenuInfoService;

	public static final String swaggerTag = "系统菜单管理";

	@AuthenticationAnnotation
	@RequestMapping(value = "/{menuId}", method = RequestMethod.GET)
	@ApiOperation(value = "获取系统菜单", httpMethod = "GET", notes = "获取系统菜单通过ID", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取系统菜单", content = "获取系统菜单通过ID")
	public ResponseEntity<ControllerResult<SysMenuInfoVo>> getSysUserInfo(
			@ApiParam(required = true, name = "menuId", value = "系统菜单ID") @PathVariable String menuId) {
		try {
			return ControllerResult.success(sysMenuInfoService.getSysMenuInfo(menuId));
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(value = "添加系统菜单", httpMethod = "POST", notes = "添加系统菜单返回添加成功的菜单ID,[该菜单用于登录后台系统]", tags = swaggerTag)
	@RequestPlanAnnotation(title = "添加系统菜单", content = "添加系统菜单返回添加成功的菜单ID,[该菜单用于登录后台系统]")
	public ResponseEntity<ControllerResult<String>> addSysMenuInfo(
			@ApiParam(required = true, name = "body", value = "系统菜单信息") @RequestBody(required = true) SysMenuInfoVo sysMenuInfo) {
		try {
			if (CommonUtil.isEmpty(sysMenuInfo.getName(), sysMenuInfo.getCode(), sysMenuInfo.getMenuDef())) {
				return ControllerResult.failed("菜单名称、菜单类型、菜单Code不允许任何一个为空");
			}

			String id = sysMenuInfoService.addSysMenuInfo(sysMenuInfo, currentUserId());
			return ControllerResult.success(id, "添加成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{menuId}", method = RequestMethod.PUT)
	@ApiOperation(value = "修改系统菜单", httpMethod = "PUT", notes = "修改系统菜单返回修改成功的菜单ID,[该菜单用于登录后台系统]", tags = swaggerTag)
	@RequestPlanAnnotation(title = "修改系统菜单", content = "修改系统菜单返回修改成功的菜单ID,[该菜单用于登录后台系统]")
	public ResponseEntity<ControllerResult<String>> updateSysMenuInfo(
			@ApiParam(required = true, name = "body", value = "系统菜单信息") @RequestBody(required = true) SysMenuInfoVo sysMenuInfo,
			@ApiParam(required = true, name = "menuId", value = "系统菜单ID") @PathVariable String menuId) {
		try {
			sysMenuInfo.setId(menuId);
			String id = sysMenuInfoService.updateSysMenuInfo(sysMenuInfo, currentUserId());
			return ControllerResult.success(id, "修改成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{menuId}", method = RequestMethod.DELETE)
	@ApiOperation(value = "删除系统菜单", httpMethod = "DELETE", notes = "删除系统菜单", tags = swaggerTag)
	@RequestPlanAnnotation(title = "删除系统菜单", content = "修改系统菜单")
	public ResponseEntity<ControllerResult<String>> deleteSysMenuInfo(
			@ApiParam(required = true, name = "menuId", value = "系统菜单ID") @PathVariable String menuId) {
		try {
			SysMenuInfoVo sysMenuInfo = sysMenuInfoService.getSysMenuInfo(menuId);
			if (null == sysMenuInfo) {
				return ControllerResult.failed("目标删除数据不存在,删除失败");
			}

			String id = sysMenuInfoService.deleteSysMenuInfo(sysMenuInfo, currentUserId());
			return ControllerResult.success(id, "删除成功");
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{menuDef}/childs", method = RequestMethod.GET)
	@ApiOperation(value = "获取子菜单", httpMethod = "GET", notes = "根据菜单id获取子菜单", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取子菜单", content = "根据菜单id获取子菜单")
	public void loadChilds(
			@ApiParam(required = true, name = "menuDef", value = "系统菜单定义[BASE_DATA_MANAGE, LOG_DATA_MANAGE]") @PathVariable SysMenuDef menuDef,
			@ApiParam(required = false, name = "parenId", value = "系统菜单ID") @RequestParam(required = false) String parenId) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/json");
			
			PrintWriter writer = response.getWriter();
			writer.print(JSONArray.toJSONString(sysMenuInfoService.childsById(parenId, menuDef)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(value = "模糊查询系统菜单数据", httpMethod = "GET", notes = "分页获取系统菜单数据", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取系统菜单数据", content = "分页获取系统菜单数据")
	public ResponseEntity<ControllerResult<PageInfo<SysMenuInfoVo>>> querySysMenuInfo(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,小于1则默认返回第一页)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = false, name = "name", value = "菜单名") @RequestParam(required = false) String name,
			@ApiParam(required = false, name = "startTime", value = "开始时间") @RequestParam(required = false) Long startTime,
			@ApiParam(required = false, name = "endTime", value = "结束时间") @RequestParam(required = false) Long endTime,
			@ApiParam(required = false, name = "code", value = "菜单编码") @RequestParam(required = false) String code,
			@ApiParam(required = false, name = "url", value = "菜单链接") @RequestParam(required = false) String url,
			@ApiParam(required = false, name = "parentId", value = "父级菜单ID") @RequestParam(required = false) String parentId,
			@ApiParam(required = false, name = "menuDef", value = "菜单类型") @RequestParam(required = false) SysMenuDef menuDef) {
		try {
			PageInfo<SysMenuInfoVo> page = sysMenuInfoService.querySysMenuInfo(name, code, url, parentId, menuDef,
					startTime, endTime, pageNumber, pageSize);
			return ControllerResult.success(page);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}
}
